/**
 * SeaResponse.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年8月15日
 */
package com.pshare.base.bean;

import java.util.ArrayList;
import java.util.List;

import org.springframework.validation.FieldError;

import com.pshare.constant.ReturnCode;

/**
 * HttpResponse 返回结果
 */
public class SeaxResponse<T> {

	public final static int TIME_OUT = -1;

	private boolean status = true;
	/** 消息 */
	private String msg;
	/** 详细消息，兼容xwx Datagram，未使用 */
	private String sub_msg;
	private Integer code = ReturnCode.SUCCESS;
	private List<Error> errors;
	private T data;

	public boolean getStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public Integer getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getSub_msg() {
		return sub_msg;
	}

	public void setSub_msg(String sub_msg) {
		this.sub_msg = sub_msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public void success() {
		this.status = true;
	}

	public void fail() {
		this.status = false;
	}

	public List<Error> getErrors() {
		return errors;
	}

	public void setErrors(List<Error> errors) {
		this.errors = errors;
	}

	public void addError(FieldError fe) {
		if (fe != null) {
			if (errors == null) {
				errors = new ArrayList<Error>();
			}
			errors.add(new Error(fe.getField(), fe.getDefaultMessage()));
		}
	}

	class Error {

		public Error() {

		}

		public Error(String field, String errMsg) {
			this.field = field;
			this.errMsg = errMsg;
		}

		private String field;
		private String errMsg;

		public String getField() {
			return field;
		}

		public void setField(String field) {
			this.field = field;
		}

		public String getErrMsg() {
			return errMsg;
		}

		public void setErrMsg(String errMsg) {
			this.errMsg = errMsg;
		}

	}

}
